package com.fanrui.code.chess.mahjong.round;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.fanrui.code.chess.mahjong.AbstractMahjongTable;
import com.fanrui.code.chess.mahjong.Mahjong;
import com.fanrui.code.chess.mahjong.MahjongGamer;
import com.fanrui.code.chess.mahjong.MahjongStandings.StandingsOperationType;
import com.fanrui.code.chess.mahjong.StandingsRound;
import com.fanrui.code.mahjong.util.MahjongCardUtil;
import com.fanrui.proto.cmd.NotifyPlayerOperation;
import com.fanrui.proto.cmd.Operation;
import com.fanrui.proto.cmd.ResPlayerOperation.Result;
import com.google.gson.annotations.Expose;

public class MahjongChiRound extends MahjongRound {

	private final static List<Operation> CANRESPONSE = Arrays.asList( Operation.SEND);
	@Expose
	private MahjongOperationResponse response;
	
	private Collection<Mahjong> operationCards;
	public MahjongChiRound(long currentId, AbstractMahjongTable<?>  table, MahjongOperationResponse response) {
		super(currentId, CANRESPONSE, table);
		this.response = response;

	}

	@Override
	public synchronized Result addResponse(MahjongOperationResponse response) {
		Result result = canOperationType(response);
		if (result != Result.Success) {
			return result;
		}
		// 判断是否能够加入到操作中
		addResponseToList(response);
		return result;
	}

	@Override
	public Object doResponse() {
		if(isDoResponse){
			return null;
		}
		isDoResponse = true;
		// 找到一个合适的响应并执行
		MahjongRound round = RoundUtil.getInstance().createRound(this,playerResponse.remove(0), table);
		table.setCurrentRound(round);
		return null;
	}

	@Override
	public void before() {
		// 移除玩家手牌中的两张牌
		MahjongGamer gamer = table.getGamerByPlayerID(currentId);
		for(Mahjong mj : response.getCards()) {
			if(operationCards == null) {
				operationCards = gamer.removeCardCheckId(mj, 1, true);
			}
			operationCards.addAll(gamer.removeCardCheckId(mj, 1, true));
		}
		Mahjong responseCard = response.getCard();
		table.removeSendCard(responseCard);
		operationCards.add(responseCard);
		gamer.addGroup(operationCards, Operation.CHI);
//		if(responseCard.getWannengType() != null) {
//			gamer.addJing(responseCard.getWannengType());
//		}
		// 通知
		NotifyPlayerOperation.Builder notify = NotifyPlayerOperation.newBuilder();
		notify.setUserID(response.getOperationId());
		notify.setType(Operation.CHI);
		List<com.fanrui.proto.cmd.Mahjong> allmj = MahjongCardUtil.getInstance().toMahjongs(operationCards);
		notify.addAllMj(allmj);
		table.broadcast(notify.build());
		table.broadcast(response, waitTime);
		table.addRound(this);
	}

	@Override
	public MahjongOperationResponse getScoreResponse() {
		return response;
	}

	@Override
	public boolean isAoutoNext() {
		return false;
	}
	@Override
	public String toString() {
		MahjongGamer gamer = table.getGamerByPlayerID(currentId);
		return String.format("玩家%s 吃牌 %s 摸牌后的手牌：%s",currentId,response.getCard(),gamer.getHandCards());
	}
	
	@Override
	public Collection<Long> getCanResponseList() {
		return Arrays.asList(currentId);
	}

	@Override
	public StandingsRound toStandingsRound() {
		return new StandingsRound(StandingsOperationType.Chi, currentId, null, operationCards);
	}
	
	@Override
	public String getInfo() {
		return String.format("牌桌ID:%s --- 玩家:%s 吃牌回合",table.getID(), currentId);
	}
}
